package syntax;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
//import com.modeliosoft.modelio.javadesigner.annotations.objid;

//@objid ("003a585e-e2dc-10da-ac48-082e5f2773a2")
public class ClockGuard implements Cloneable, Comparable<ClockGuard> {
    //@objid ("007ca808-e3e5-10da-ac48-082e5f2773a2")

    private List<ClockConditionAtom> conditionAtom;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        //ClockGuard res = new ClockGuard((LinkedList<ClockConditionAtom>)(((LinkedList<ClockConditionAtom>)(conditionAtom)).clone()));
        ClockGuard res = new ClockGuard();
        res.setConditionAtom(new LinkedList<ClockConditionAtom>());
        for (ClockConditionAtom atom : conditionAtom) {
            res.addClockConditionAtom((ClockConditionAtom) (atom.clone()));
        }
        return res;
    }

    public ClockGuard() {
        conditionAtom = new LinkedList<ClockConditionAtom>();
    }

    public ClockGuard(List<ClockConditionAtom> conditionAtom) {
        this.conditionAtom = conditionAtom;
    }

    public List<ClockConditionAtom> getConditionAtom() {
        return conditionAtom;
    }

    public void setConditionAtom(List<ClockConditionAtom> conditionAtom) {
        this.conditionAtom = conditionAtom;
    }

    public void addClockConditionAtom(ClockConditionAtom c) {
        conditionAtom.add(c);
    }

    public boolean isEmpty() {
        return getConditionAtom().isEmpty();
    }
    
    public boolean containsClock(Clock clock) {
        for (ClockConditionAtom atom : conditionAtom) {
            if (atom.getClock().equals(clock)) {
                return true;
            }
        }
        return false;
    }

    @Override
    public String toString() {
        //return super.toString();
        Iterator iteratorClock;
        StringBuilder bf = new StringBuilder();
        if (!conditionAtom.isEmpty()) {
            iteratorClock = conditionAtom.iterator();
            bf.append(iteratorClock.next().toString());
            while (iteratorClock.hasNext()) {
                bf.append(",");
                bf.append(iteratorClock.next().toString());
            }
        }
        return bf.toString();
    }

    @Override
    public int compareTo(ClockGuard t) {
        Collections.sort(conditionAtom);
        Collections.sort(t.conditionAtom);
        int res = 0;
        Iterator<ClockConditionAtom> iterThis, iterOther;
        iterThis = this.getConditionAtom().iterator();
        iterOther = t.getConditionAtom().iterator();
        ClockConditionAtom thisAtom, otherAtom;
        while (iterThis.hasNext() && iterOther.hasNext()) {
            thisAtom = iterThis.next();
            otherAtom = iterOther.next();
            res = thisAtom.compareTo(otherAtom);
            if (res != 0) {
                return res;
            }
        }
        if (iterThis.hasNext()) {
            return 1;
        }
        if (iterOther.hasNext()) {
            return -1;
        }
        return 0;
    }
}
